package ch3.Singleton;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 基于原子变量类的指标统计器
 * 分布式系统的性能测试桩（Test stub)需要记录在测试过程中接收到的请求总数、处理成功数和处理失败数这三个指标
 */

public class Indicator {
    private static final Indicator INSTANCE = new Indicator();

    // 记录请求总数
    private final AtomicLong requestCount = new AtomicLong(0);
    // 记录处理成功总数
    private final AtomicLong successCount = new AtomicLong(0);
    // 记录处理失败总数
    private final AtomicLong failureCount = new AtomicLong(0);

    private Indicator() {}

    public static Indicator getInstance() {
        return INSTANCE;
    }

    public void newRequestReceived() {
        requestCount.incrementAndGet();
    }

    public void newRequestProcessed() {
        successCount.incrementAndGet();
    }

    public void requestProcessedFailed() {
        failureCount.incrementAndGet();
    }

    public long getRequestCount() {
        return requestCount.get();
    }

    public long getSuccessCount() {
        return successCount.get();
    }

    public long getFaiureCount() {
        return failureCount.get();
    }
}
